package com.alibaba.citrus.ots.application.report.ability.utils;

import com.epoch.app.otsapplicationreportsdk.model.dto.MonthSalesBasicReportSDO;
import com.google.common.base.Objects;
import com.google.common.collect.Lists;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2022/1/17 2:31
 */
public class ObjectEqualsUtil {

    public static boolean domainEquals(Object source, Object target, List<String> equalFields) {
        if (source == null || target == null) {
            return false;
        }
        boolean rv = false;
        if (source instanceof Map || target instanceof Map ) {
            return rv;
        } else if (source instanceof List || target instanceof List ){
            return rv;
        } else {
            rv = classOfSrc(source, target, equalFields);
        }
        //logger.info("THE EQUALS RESULT IS " + rv);
        return rv;
    }

    private static boolean classOfSrc(Object source, Object target, List<String> equalFields) {
        Class<?> srcClass = source.getClass();
        Field[] fields = srcClass.getDeclaredFields();
        for (Field field : fields) {
            String nameKey = field.getName();
            if(equalFields.contains(nameKey)){
                Object srcValue = getClassValue(source, nameKey);
                Object tarValue = getClassValue(target, nameKey);
                if(srcValue instanceof BigDecimal && tarValue instanceof BigDecimal){
                    if(((BigDecimal) srcValue).compareTo((BigDecimal) tarValue) == 0){
                        continue;
                    }else{
                        return false;
                    }
                }else {
                    if(Objects.equal(srcValue,tarValue)){
                        continue;
                    }else{
                        return false;
                    }
                }
            }
        }
        return true;
    }

    private static Object getClassValue(Object obj, String fieldName){
        Field field = null;
        try {
            field = obj.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) {
        MonthSalesBasicReportSDO newdata = new MonthSalesBasicReportSDO();
        newdata.setTaskCount(123l);
        newdata.setMonthMoney(new BigDecimal("408230.80"));
        MonthSalesBasicReportSDO olddata = new MonthSalesBasicReportSDO();
        olddata.setTaskCount(122l);
        olddata.setMonthMoney(new BigDecimal("408230.8"));

        System.out.println(ObjectEqualsUtil.domainEquals(newdata,olddata, Lists.newArrayList("monthMoney")));
    }

}
